package com.kc.web.controller.business;

import com.kc.business.domain.KcHistoricalFigure;
import com.kc.business.service.KcHistoricalFigureService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.domain.model.LoginUser;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import com.kc.common.utils.ServletUtils;
import com.kc.framework.web.service.TokenService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * FileName: KcHistoricalFigureController
 * Author:   TP
 * Date:     2020-11-09 14:55
 * Description: 历史人物控制器
 */
@RestController
@AllArgsConstructor
@RequestMapping("/business/historicalFigure")
public class KcHistoricalFigureController extends BaseController {

    private final KcHistoricalFigureService kcHistoricalFigureService;
    private final TokenService tokenService;

    /**
     * 获取历史人物信息列表
     */
    @GetMapping("/list")
    public TableDataInfo getList(KcHistoricalFigure historicalFigure) {
        startPage();
        List<KcHistoricalFigure> list = kcHistoricalFigureService.getList(historicalFigure);
        return getDataTable(list);
    }

    /**
     * 获取历史人物信息详情
     */
    @GetMapping("/info/{id}")
    public AjaxResult getInfo(@PathVariable Integer id) {
        if (id != null && id > 0) {
            return AjaxResult.success(kcHistoricalFigureService.getById(id));
        }
        return AjaxResult.error("请选择一条记录");
    }

    /**
     * 新增历史人物
     */
    @RepeatSubmit
    @Log(title = "新增历史人物", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('business:historicalFigure:add')")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody KcHistoricalFigure figure) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser != null) {
            figure.setCreateBy(loginUser.getUsername());
            figure.setCreateTime(LocalDateTime.now());
            return kcHistoricalFigureService.add(figure);
        }
        return AjaxResult.error("操作失败，未获取到当前登陆人信息");
    }

    /**
     * 修改历史人物
     */
    @RepeatSubmit
    @Log(title = "修改历史人物", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('business:historicalFigure:update')")
    @PutMapping("/update")
    public AjaxResult update(@Validated @RequestBody KcHistoricalFigure figure) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser != null) {
            figure.setUpdateBy(loginUser.getUsername());
            figure.setUpdateTime(LocalDateTime.now());
            return kcHistoricalFigureService.update(figure);
        }
        return AjaxResult.error("操作失败，未获取到当前登陆人信息");
    }

    /**
     * 删除历史人物
     */
    @Log(title = "删除历史人物", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('business:historicalFigure:delete')")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        if (id != null && id > 0) {
            if (kcHistoricalFigureService.deleteById(id) > 0) {
                return AjaxResult.success();
            }
            return AjaxResult.error("操作失败，数据库删除失败");
        }
        return AjaxResult.error("操作失败，请指定要删除的历史人物");
    }

}
